<?php

class ThinkWechatMenu {
	
    private $fromUsername;
    private $toUsername;
    private $times;
    private $keyword;
    private $app_id;
    private $app_secret;
    
    
    public function __construct($appid,$appsecret) {
        # code...
        $this->app_id = $appid;
        $this->app_secret = $appsecret;
    }

    /**
     * 运行程序
     * @param string $value [description]
     */
    public function Run($menu){
    	
        $access_token = $this->get_access_token();//获取access_token
        return $this->createmenu($access_token,$menu);//创建菜单
    }
    
    /**
     * 获取access_token
     */

    private function get_access_token(){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->app_id."&secret=".$this->app_secret;
        $data = json_decode(file_get_contents($url),true);
        if($data['access_token']){
            return $data['access_token'];
        }else{
            return "access_token error";
        }
    }
    
    /**
     * 获取用户个人资料
     */
    public function userDetail($openid){
    	 
    	$access_token = $this->get_access_token();
    	$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid;
      	 
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL,$url);
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    	//curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Content-length: ".strlen($jsondata)));
    	//curl_setopt($ch,CURLOPT_POST,1);
    	//curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
    	$output = curl_exec($ch);
    	curl_close($ch);
    	return $output;
    }
    
    /**
     * 创建二维码
     */
    public function qrcode($menu){
    	 	
    	$access_token = $this->get_access_token();
    	$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    	/* $menu = array(
    			'action_name'=>'QR_LIMIT_SCENE',
    			'action_info'=>array('scene'=>array('scene_id'=>1)),
    	); */
    	$jsondata = JSON($menu);
    	
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL,$url);
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    	curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Content-length: ".strlen($jsondata)));
    	curl_setopt($ch,CURLOPT_POST,1);
    	curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
    	$output = curl_exec($ch);
    	curl_close($ch);
    	//echo "<br>a:::::::::".$output;
    	return $output;
    }

    /**
     * 创建菜单
     */
    public function createmenu($access_token,$menu){		
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $arr = array( 
            'button' =>$menu
        );

        $jsondata = JSON($arr);
		
		$ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Content-length: ".strlen($jsondata))); 
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
        $output = curl_exec($ch);
        curl_close($ch);
		return $output;
    }
}

/**************************************************************
 *
 *	使用特定function对数组中所有元素做处理
 *	@param	string	&$array		要处理的字符串
 *	@param	string	$function	要执行的函数
 *	@return boolean	$apply_to_keys_also		是否也应用到key上
 *	@access public
 *
 *************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false){
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }
 
        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}
 
/**************************************************************
 *
 *	将数组转换为JSON字符串（兼容中文）
 *	@param	array	$array		要转换的数组
 *	@return string		转换得到的json字符串
 *	@access public
 *
 *************************************************************/
function JSON($array) {
	arrayRecursive($array, 'urlencode', true);
	$json = json_encode($array);
	return urldecode($json);
}
?>